﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Entities;
using BLL;

namespace Stationery.UC
{
    public partial class UC_NewsDetail : System.Web.UI.UserControl
    {
        BLL_News bll_News = new BLL_News();
        protected void Page_Load(object sender, EventArgs e)
        {
            int group_id = 0;
            group_id = Convert.ToInt32(Request.Params["gid"]);
            if (group_id == 0)
            {
                return; //or Invalid Page
            }
            int news_id = 0;
            news_id = Convert.ToInt32(Request.Params["newsid"]);
            if (news_id == -1)
                return; //or Invalid Page
            DataSet ds = new DataSet();
            ds = bll_News.Get_NewsDetail(group_id,news_id);
                        
            if (ds == null)
            {
                return; //Db Error
            }
            else
            {
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    return;// or Invalid page
                }
                else
                {
                    HyperLinkGroupNews.Text = ds.Tables[0].Rows[0]["Group_name"].ToString();
                    HyperLinkGroupNews.NavigateUrl = "~/News.aspx?viewtype=groupnews&gid=" + ds.Tables[0].Rows[0]["Group_id"].ToString();
                    ASPxLabelTitle.Text = ds.Tables[0].Rows[0]["Title"].ToString();
                    DateTime date = (DateTime)ds.Tables[0].Rows[0]["Create_date"];
                    LabelNewsDate.Text = date.ToString("dd/MM/yyyy");
                    DivDetail.InnerHtml = ds.Tables[0].Rows[0]["Content_detail"].ToString();
                    DataListTitle.DataSource = ds.Tables[1];
                    DataListTitle.DataBind();  
                }                 
            }
        }

        protected void DataListTitle_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item != null)
            {
                DataRowView dtrv = (DataRowView)e.Item.DataItem;

                HyperLink hltitle = (HyperLink)e.Item.FindControl("HyperLinkTitle");
                hltitle.Text = dtrv["Title"].ToString();
                hltitle.NavigateUrl = "~/News.aspx?viewtype=detail&gid=" + dtrv["Group_id"].ToString()+ "&newsid=" + dtrv["News_ID"].ToString();
                Label label = (Label)e.Item.FindControl("LabelDate");
                DateTime dt = (DateTime)dtrv["Create_date"];
                label.Text = "(" + dt.ToString("dd/MM") + ")";
            }
        }
    }
}